<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css"
          href="../easyui/themes/bootstrap/easyui.css">
    <link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../easyui/demo/demo.css">
    <script type="text/javascript" src="../easyui/jquery.min.js"></script>
    <script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../myjs/path.js"></script>
    <script src="../js/yangshi.js"></script>
</head>
<style type="text/css">
    .datagrid-footer {
        background-color: #f7c66c;
    }
</style>
<body class="easyui-layout" style="width: 100%; height: 100%;">


<div id="tb">
    <strong>账期：</strong>
    <input id="periodId" class="easyui-combobox" style="width: 160px;" data-options="editable:false,panelHeight:'auto'">
    <input id="bumeng"  style="width: 200px;" class="easyui-combotree">
    <input id="key" class="easyui-searchbox" data-options="prompt:'输入客户名称搜索',searcher: doSearch"
           style="width: 200px"></input>
   <%-- <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true"
       onclick="congzhi()">重置搜索</a>--%>
    <a iconCls="icon-save" class="easyui-linkbutton" type="button" onclick="saveToExcel()">导出总账</a>

</div>
<div id="tb1">
    所属年季:<input class="easyui-combobox" id="naj" data-options="panelHeight:'auto'" >
    日期从:<input class="easyui-datebox" id="qtime" style="width: 120px">
          到:<input class="easyui-datebox" id="etime" style="width: 120px">
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="sea()">搜索</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="chongzhi()">重置搜索</a>
    <a iconCls="icon-save" class="easyui-linkbutton" type="button" onclick="saveToExcelMX()">导出明细</a>
</div>
<div data-options="region:'north',split:true" style="width: 100%; height: 70%">

    <table id="cxdm">
    </table>

</div>

<div data-options="region:'center',title:'财务明细',split:true" style="width: 100%; height: 30%">
    <table id="zzmx">
    </table>

</div>


<input id='jgid' type="hidden" value="<%=request.getParameter("jgid")%>">
<input id='username' type="hidden" value="<%=request.getParameter("username")%>">
<input id='userid' type="hidden" value="<%=request.getParameter("userid")%>">

</body>
</html>
<script>

    function sea() {
        loadChildren($('#cxdm').datagrid("getSelected").id,$('#cxdm').datagrid("getSelected").wldwId,$('#naj').combobox("getValue"))
    }
    function chongzhi(){
        $("#naj").combobox("clear")
        $('#qtime').combo('setValue', '');
        $('#etime').combo('setValue', '');
        $('#etime').combo('clear');
        $('#qtime').combo('clear');
        loadChildren($('#cxdm').datagrid("getSelected").id,$('#cxdm').datagrid("getSelected").wldwId,$('#naj').combobox("getValue"))
    }
    $(function(){$('#naj').combobox({
            url:path+"/finance/findnaj.do",
            valueField:'yearandseason',
            textField:'yearandseason',
            loadFilter : function(data) {
                console.log(data)
                return data.data
            }/*/!*,onSelect: function (node) {

                loadChildren($('#cxdm').datagrid("getSelected").id,$('#cxdm').datagrid("getSelected").wldwId,node.yearandseason)
            }**/
        })

    })

    function doSearch(value) {
        loadGrid(value)
    }

    $(function () {

        $("#periodId").combobox({
            url: path + "/finance/findperiodlist.do",
            valueField: 'id',
            textField: 'name',
            onLoadSuccess: function () {  // 加载完成后,设置选中第一项
                var val = $(this).combobox("getData");
                for (var item in val[0]) {
                    if (item == "id") {
                        $(this).combobox("select", val[0][item]);
                    }
                }

                $('#bumeng').combotree({
                    url: path + '/jg/tree.do',
                    required: true,
                    loadFilter: function (data) {
                        return data.rows
                    }, onLoadSuccess: function (node, data) {
                        if (data.length > 0) {
                            $("#bumeng").combotree('setValue', data[0].id);
                        }
                        loadGrid('')
                    }, onClick: function (node) {

                        loadGrid('')
                    }
                })

            },
            loadFilter: function (data) {
                return data.data

            }
        })


    })
     //导出总账信息
    function saveToExcel() {
        var shujuSize = $("#cxdm").datagrid("getRows").length;
        if (shujuSize == 0) {
             alert("暂无数据!")
            return;
        }

        window.location.href = path+"/finance/zongzhangdaochu.xls?jgId="+ $('#bumeng').combotree('getValue') + "&key=" + $("#key").searchbox('getValue')+"&periodId=" + $('#periodId').combobox('getValue')
    }
    //导出总账子信息
    function saveToExcelMX() {
        var shujuSize = $("#zzmx").datagrid("getRows").length;
        if (shujuSize == 0) {
            alert("暂无数据!")
            return;
        }
        var etime = $("#etime").val();
        if (null != etime && '' != etime) {
            etime += " 23:59:59";
        }
        window.location.href = path+"/finance/zongzhangdaochumx.xls?jgId="+ $('#bumeng').combotree('getValue')
            + "&nj=" + $("#naj").combobox('getValue')
            +"&periodId=" + $('#periodId').combobox('getValue')
            +"&wldwId="+$('#cxdm').datagrid("getSelected").wldwId
            +"&financeId="+$('#cxdm').datagrid("getSelected").id
            +"&stime="+$('#qtime').val()
            +"&etime="+etime
    }

//加载总账主表数据
    function loadGrid(key) {
        $("#cxdm").datagrid({
            url: path + '/finance/findzzlist.do',
            queryParams: {
                jgId: $('#bumeng').combotree('getValue'),
                key: key,
                periodId:$('#periodId').combobox('getValue')
            }
        })
    }
//加载子表数据
    function loadChildren(financeId,wldwId,nj){
        var etime = $("#etime").val();
        if (null != etime && '' != etime) {
            etime += " 23:59:59";
        }
        $("#zzmx").datagrid({
            url: path + '/finance/findzzmxlist.do',
            queryParams: {
                periodId: $("#periodId").combobox("getValue"),
                jgId: $('#bumeng').combotree('getValue'),
                wldwId:wldwId,
                financeId:financeId,
                stime:$('#qtime').val(),
                etime:etime,
                nj:nj
            }
        })
    }


    $(document).keyup(function (event) {
     if (event.keyCode == 13) {
     loadGrid();
     }
     });
    $('#cxdm').datagrid(
        {
            width: 'auto',
            toolbar: '#tb',
            //height : 340,
            singleSelect: true,
            loadMsg: '数据加载中请稍后……',
            pagination: true,
            rownumbers: true,
            sortName: 'amount',
            sortOrder: 'asc',
            pageSize: 20,//分页大小
            fitColumns: true,
            striped: true,
            collapsible: true,
            pageList: [10, 20, 50, 100],//每页的个数
            fit: true,//自动补全
            showFooter: true,
            columns: [[
                {
                    field: 'id',
                    title: '编号（需要隐藏）',
                    width: 50,
                    align: 'right',
                    hidden: true
                }, {
                    field: 'wldwId',
                    title: '往来单位（需要隐藏）',
                    width: 50,
                    align: 'right',
                    hidden: true
                },
                {
                    field: 'wldwName',
                    title: '往来单位',
                    width: 200,
                    align: 'center'
                }, {
                    field: 'khtype',
                    title: '往来单位类型',
                    width: 100,
                    align: 'center' ,
                    formatter: function (value) {
                        if(value==0){
                            return "批发"
                        }else if(value==1){
                            return "分销"
                        }else if(value==2){
                            return "零售会员"
                        }else if(value==3){
                            return "供应商"
                        }else{
                            return "其他"
                        }

                    }
                },
                {
                    field: 'startMoney',
                    title: '期初金额',
                    width: 120,
                    align: 'right',
                    formatter: function (value) {
                        if (value == 0)
                            return value;
                        return value.toFixed(2);
                    }
                },
                {
                    field: 'amount',
                    title: '本期交易金额',
                    width: 120,
                    sortable: true,
                    align: 'right',
                    formatter: function (value) {
                        if (value == 0)
                            return value;
                        return value.toFixed(2);
                    }
                },
                {
                    field: 'paid',
                    title: '已收金额',
                    width: 120,
                    align: 'right',
                    sortable: true,
                    formatter: function (value) {
                        if (value == 0)
                            return value;
                        return value.toFixed(2);
                    }
                },
                {
                    field: 'qiankuan',
                    title: '欠款',
                    width: 120,
                    sortable: true,
                    align: 'right',
                    formatter: function (value) {
                        if (value != null) {
                            return (value).toFixed(2);
                        } else {
                            return value;
                        }
                    }
                },
                {
                    field: 'cheap',
                    title: '优惠金额',
                    width: 120,
                    align: 'right',
                    formatter: function (value) {
                        if (value == 0)
                            return value;
                        return value.toFixed(2);
                    }
                }
            ]], onSelect: function (index, row) {
                loadChildren(row.id,row.wldwId,$('#naj').combobox('getValue'))
        },
        onLoadSuccess: function (data) {

        $('#cxdm').datagrid("selectRow", 0);


    }
        });


    $('#zzmx').datagrid(
        {
            width: 'auto',
            toolbar: '#tb1',
            singleSelect: true,
            loadMsg: '数据加载中请稍后……',
            pagination: true,
            rownumbers: true,
            pageSize: 20,//分页大小
            fitColumns: true,
            striped: true,
            collapsible: true,
            pageList: [10, 20, 50, 100],//每页的个数
            fit: true,//自动补全
            showFooter: true,
            columns: [[
                {
                    field: 'id',
                    title: '编号（需要隐藏）',
                    width: 50,
                    align: 'right',
                    hidden: true
                }, {
                    field: 'wldwName',
                    title: '往来单位',
                    width: 50,
                    align: 'center'
                }, {
                    field: 'khtype',
                    title: '往来单位类型',
                    width: 100,
                    align: 'center' ,
                    formatter: function (value) {
                        if(value==0){
                            return "批发"
                        }else if(value==1){
                            return "分销"
                        }else if(value==2){
                            return "零售会员"
                        }else if(value==3){
                            return "供应商"
                        }else{
                            return "其他"
                        }
                    }
                }, {
                    field: 'yddid',
                    title: '订单号',
                    width: 50,
                    align: 'center',
                    hidden: true
                }, {
                    field: 'hl',
                    title: '红蓝',
                    width: 50,
                    align: 'center',
                    hidden: true
                }, {
                    field: 'fslx',
                    title: '类型',
                    width: 50,
                    align: 'center',formatter: function (value,row) {
                       if((value==null || value=='')){
                           if( row.hl==1){
                               return '收帐'
                           }else{
                               return '出帐'
                           }

                       }else{
                           return value
                       }
                    }
                },{
                    field: 'yearandseason',
                    title: '所属年季',
                    width: 200,
                    align: 'center'
                },{
                    field: 'zshuliang',
                    title: '总数',
                    width: 120,
                    align: 'right',
                    formatter: function (value) {
                       return value
                    }
                },
                {
                    field: 'amount',
                    title: '金额',
                    width: 120,
                    align: 'right',
                    formatter: function (value) {
                        if (value == 0)
                            return value;
                        return value.toFixed(2);
                    }
                }, {
                    field: 'time',
                    title: '时间',
                    width: 50,
                    align: 'center'
                }
            ]]
        });


</script>